<?php


/*
 * Created on 2011-4-24
 */

/**
 * Plugin Name: Business Index
 * Plugin URI: http://www.freeindex.ca/wordpress-plugins/business-index
 * Description: A plugin to build business directory.
 * Version: 1.0.0
 * Author: Dustin Zhang & Martin Zhang
 * Author URI: http://www.freeindex.ca
 * License: GPLv2 or later
 */

/**
 * Copyright 2011  Freeindex  (email : freeindex.ca@gmail.com)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/**
 * Hooks to activate the plugin
 */
register_activation_hook(__FILE__, 'dl_bi_activation');

function dl_bi_activation() {
	// Check the WP core version compatibility
	if (version_compare(get_bloginfo('version'), '3.1', '<')) {
		deactivate_plugin(basename(__FILE__));
	}
	
	// Add the rewrite rule and flush
	dl_bi_add_business_rewrite_rules();
    flush_rewrite_rules();
}

// Add the rewrite rule
add_action( 'init', 'dl_bi_add_business_rewrite_rules' );
function dl_bi_add_business_rewrite_rules() {
    add_rewrite_rule( 'business/?([^/]*)',
        'index.php?business=$matches[1]', 'top' );
   
}

/**
 * Hook to de-activate the plugin
 */
register_deactivation_hook(__FILE__, 'dl_bi_deactivation');
function dl_bi_deactivation() {
	flush_rewrite_rules();
}

/**
 * Hook to test rewrite rules
 */
// add_action('template_redirect','dl_bi_rewrite_show_rules');
//  function dl_bi_rewrite_show_rules(){
//  	$tax = get_query_var( 'taxonomy' );
//  	$term = get_query_var( 'term' );
//  	echo "tax=> $tax, term=> $term ";
//  	//exit;
//  }

/**
 * Hooks to define a new post type - Business
 */
add_action('init', 'dl_bi_register_business');

function dl_bi_register_business() {
	/* Set up the arguments for the 'music_album' post type. */
	$business_args = array (
		'public' => true,
		'query_var' => 'business',
		'rewrite' => array (
			'slug' => '',
			'with_front' => false,
		),
		'supports' => array (
			//'title',
			'thumbnail'
		),
		'labels' => array (
			'name' => 'Business',
			'singular_name' => 'Business',
			'add_new' => 'Add New Business',
			'add_new_item' => 'Add New Business',
			'edit_item' => 'Edit Business',
			'new_item' => 'New Business',
			'view_item' => 'View Business',
			'search_items' => 'Search Business',
			'not_found' => 'No Business Found',
			'not_found_in_trash' => 'No Business Found In Trash'
		),
		//        'capability_type' => 'business'

	
	);

	/* Register the music album post type. */
	register_post_type('business', $business_args);
}

/**
 * Hooks to add a company meta-box in company admin 
 */
add_action('add_meta_boxes', 'dl_bi_business_meta_create');

function dl_bi_business_meta_create() {
	add_meta_box('business-meta', 'Business Information', 'dl_bi_business_info', 'business', 'normal', 'high');
}

/**
 * Display the business field in business edit page
 */
function dl_bi_business_info($post) {
	$postid = $post->ID;
	$company_name = get_post_meta($postid, 'dl_bi_company_name', true);
	$company_street = get_post_meta($postid, 'dl_bi_company_street', true);
	$company_phone = get_post_meta($postid, 'dl_bi_company_phone', true);
	$company_website = get_post_meta($postid, 'dl_bi_company_website', true);
	$company_email = get_post_meta($postid, 'dl_bi_company_email', true);
?>
<table class="form-table">	
<tr valign="top">
	<th scope="row">
		<label for="dl_bi_company_name"><?php _e('Company name', 'business-index'); ?><span class="description"><?php _e('(required)'); ?></span></label>
	</th>
	<td>
		<input class="regular-text" type="text" name="dl_bi_company_name" value="<?php echo esc_attr($company_name); ?>"/>
	</td>
</tr>

<tr>
	<th scope="row">
		<label for="dl_bi_company_phone"><?php _e('Phone', 'business-index'); ?><span class="description"><?php _e('(required)'); ?></span></label>
	</th>
	<td><input type="text" name="dl_bi_company_phone" value="<?php echo esc_attr($company_phone); ?>"/>
	</td>
</tr>

<tr>
	<th><label for="dl_bi_company_email"><?php _e('E-mail', 'business-index'); ?></label></th>
	<td><input class="regular-text" type="text" name="dl_bi_company_email" value="<?php echo esc_attr($company_email); ?>" />
</tr>

<tr>
	<th scope="row">
		<label for="dl_bi_company_street"><?php _e('Street', 'business-index'); ?><span class="description"><?php _e('(required)'); ?></span></label>
	</th>
	<td><input class="regular-text" type="text" name="dl_bi_company_street" value="<?php echo esc_attr($company_street); ?>"/>
	</td>
</tr>

<tr>
	<th><label for="dl_bi_company_website"><?php _e('Website', 'business-index') ?></label></th>
	<td><input class="regular-text code" type="text" name="dl_bi_company_website" value="<?php echo esc_attr($company_website) ?>"/></td>
</tr>


</table>
	<?php


}

/**
 * Hook to update the comapny info
 */
add_action('save_post', 'dl_bi_update_company_info');

function dl_bi_update_company_info($postid) {
	if (isset ($_POST['dl_bi_company_name'])) {
		$company_name = strip_tags($_POST['dl_bi_company_name']);
		update_post_meta($postid, 'dl_bi_company_name', $company_name);
		// set the title to company name
		global $wpdb;
		$updated_title = array (
			'post_title' => $company_name
		);
		$wpdb->update($wpdb->posts, $updated_title, array (
			'ID' => $postid
		));
	}
	if (isset ($_POST['dl_bi_company_phone'])) {
		$company_phone = strip_tags($_POST['dl_bi_company_phone']);
		update_post_meta($postid, 'dl_bi_company_phone', $company_phone);
	}
	if (isset ($_POST['dl_bi_company_street'])) {
		$company_street = strip_tags($_POST['dl_bi_company_street']);
		update_post_meta($postid, 'dl_bi_company_street', $company_street);
	}
	if (isset ($_POST['dl_bi_company_website'])) {
		$company_website = strip_tags($_POST['dl_bi_company_website']);
		update_post_meta($postid, 'dl_bi_company_website', $company_website);
	}
	if (isset ($_POST['dl_bi_company_email'])) {
		$company_email = strip_tags($_POST['dl_bi_company_email']);
		update_post_meta($postid, 'dl_bi_company_email', $company_email);
	}
}

/**
 * Hook to register Taxonomies for business
 */
add_action('init', 'dl_bi_register_business_taxonomies', 0);

function dl_bi_register_business_taxonomies() {

	/* Set up the Location taxonomy arguments. */
	$location_args = array (
		'hierarchical' => true,
		'query_var' => 'location',
		'show_tagcloud' => false,
		'rewrite' => array (
			'slug' => 'location',
			'hierarchical' => true,
			'with_front' => false
		),
		'labels' => array (
			'name' => 'Location',
			'singular_name' => 'Location',
			'edit_item' => 'Edit Location',
			'update_item' => 'Update Location',
			'add_new_item' => 'Add New Location',
			'new_item_name' => 'New Location Name',
			'all_items' => 'All Locations',
			'search_items' => 'Search Locations',
			'parent_item' => 'Parent Location',
			'parent_item_colon' => 'Parent Location:',			
		),		
	);

	/* Register the business location taxonomy. */
	register_taxonomy('location', 'business', $location_args);

	$category_args = array (
		'hierarchical' => true,
		'query_var' => 'business_category',
		'show_tagcloud' => false,
		'rewrite' => array (
			'slug' => 'cat',
			'hierarchical' => true,
			'with_front' => false
		),
		'labels' => array (
			'name' => 'Categories',
			'singular_name' => 'Category',
			'edit_item' => 'Edit Category',
			'update_item' => 'Update Category',
			'add_new_item' => 'Add New Category',
			'new_item_name' => 'New Category Name',
			'all_items' => 'All Categories',
			'search_items' => 'Search Categories',
			'parent_item' => 'Parent Category',
			'parent_item_colon' => 'Parent Category:',
		),		
	);

	/* Register the business category taxonomy. */
	register_taxonomy('business_category', 'business', $category_args);
}

/**
 * Reome quick edit for business post type
 */
add_filter('post_row_actions', 'remove_business_row_actions', 10, 1);

function remove_business_row_actions($actions) {
	if (get_post_type() === 'business')
		//unset( $actions['edit'] );
		//unset( $actions['view'] );
		//unset( $actions['trash'] );
		unset ($actions['inline hide-if-no-js']);
	return $actions;
}


?>
